package docks;

import java.util.Hashtable;
import java.util.List;

import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import persistence.DataBaseController;
import utils.ShowException;

public class DockController {

    private static DockAPI _api;
    // private static Hashtable<Integer, Dock> _starred;
    private static DockController _instance;
    private static Context ctx;

    private DockController() {

	// _starred = new Hashtable<Integer, Dock>();
    }

    public static DockController getInstance() {

	if (_instance == null) {

	    _instance = new DockController();
	    _api = new DockAPIImpl();
	}
	return _instance;
    }

    public static DockController getInstance(Context ctx) {

	DockController.ctx = ctx;

	if (_instance == null) {

	    _instance = new DockController();
	    _api = new DockAPIImpl();
	}
	return _instance;
    }

    public void restartController() {

	_instance = null;
    }

    public List<Dock> getAllDocks() {

	return _api.getAllDocks();
    }
    
    public List<Dock> getFullDocks() {
	
	return _api.getFullDocks();
    }
    
    public List<Dock> getHalfDocks() {
	
	return _api.getHalfDocks();
    }
    
    public List<Dock> getEmptyDocks() {
	
	return _api.getEmptyDocks();
    }

    public List<Dock> getStarredDocks() {

	return _api.getAllStarred();
    }

    public Dock getDock(int dockId) {

	return _api.getDockInfo(dockId);
    }

    public void changeStarred(int dockId) {

	Dock d = _api.getDockInfo(dockId);
	Log.v("CHIVATO", "Cambiando starred!!");
	if (!d.isStarred()) {

	    DataBaseController.getInstance().insertFavorite(dockId);
	} else {

	    DataBaseController.getInstance().deleteFavorite(dockId);
	}
    }

    public void refreshDockData(Hashtable<Integer, Dock> data) {
	
	_api.refreshData(data);
    }
    
    public Dock getClosestDockToPark(GeoPoint p) {
	
	return _api.getClosestDock(p,true);
    }
    
    public Dock getClosesDockToPickUp(GeoPoint p) {
    	
    return _api.getClosestDock(p, false);	
    }
}
